package com.example.yiwei.crm.view.activity.guideActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.entity.ApointmentEntity;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class hospitalGuideActivity extends AppCompatActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{
    private AutoListView listView;
    private MyAdapter adapter;
    List<ApointmentEntity> Appointlist;
    List<ApointmentEntity> list;
    int appoint_id;
    int page=0;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<ApointmentEntity> result = (List<ApointmentEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    listView.onRefreshComplete();
                    Appointlist.addAll(result);
                    break;
                case AutoListView.LOAD:
                    listView.onLoadComplete();
                    Appointlist.addAll(result);
                    break;
            }
//            listView.setResultSize(result.size());
            adapter.notifyDataSetChanged();

        }

        ;
    };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_guide);
        listView= (AutoListView) findViewById(R.id.hospital_guide_listview);
        listView.setOnRefreshListener(this);
        listView.setOnLoadListener(this);
        Appointlist = new ArrayList<>();
        InitTask initTask= new InitTask();
        initTask.execute();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                appoint_id=Appointlist.get(i).getAppoint_id();
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                    }
                }).start();
            }
        });

    }
    private void loadData(int refresh) {
        if(refresh==AutoListView.LOAD){
            LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }else if (refresh==AutoListView.REFRESH){
            UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }

    }

    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            customerDao customerdao = new customerDaoImpl();
            list=new ArrayList<ApointmentEntity>();
            list=customerdao.queryTriageMessage(baseClass.query_all_appointment_message+1,null);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {

            msg.obj = list;
            handler.sendMessage(msg);
            super.onPostExecute(integer);
        }
    }
    class InitTask extends AsyncTask<Void,Integer,Integer> {
        @Override
        protected Integer doInBackground(Void... voids) {
            customerDao customerdao = new customerDaoImpl();
            Appointlist=customerdao.queryTriageMessage(baseClass.query_all_appointment_message+1,null);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            adapter = new MyAdapter();
            listView.setAdapter(adapter);
        }
    }
    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {

            int what = 1;
            msg.what = what;
            page=page+1;
            customerDao customerDao=new customerDaoImpl();
            list=new ArrayList<ApointmentEntity>();
            list=customerDao.queryTriageMessage(baseClass.query_all_appointment_message+page,null);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {

            msg.obj = list;
            handler.sendMessage(msg);
            super.onPostExecute(integer);
        }
    }
    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return Appointlist.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view==null){
                view=getLayoutInflater().inflate(R.layout.triage_message,null);
                holder=new ViewHolder();
                holder.txt_date= (TextView) view.findViewById(R.id.triage_appointment_date);
                holder.txt_state= (TextView) view.findViewById(R.id.triage_appointment_state);
                holder.txt_name= (TextView) view.findViewById(R.id.triage_customer_name);
                holder.txt_number= (TextView) view.findViewById(R.id.triage_appointment_number);
                view.setTag(holder);
            }else{
                holder= (ViewHolder) view.getTag();
            }
            holder.txt_number.setText(Appointlist.get(i).getAppointment_number());
            holder.txt_name.setText(Appointlist.get(i).getCustomer_name());
            if (Appointlist.get(i).getAppointment_state()==0){
                holder.txt_state.setText("(未预约)");
            }else if (Appointlist.get(i).getAppointment_state()==1){
                holder.txt_state.setText("(已预约)");
            }else if (Appointlist.get(i).getAppointment_state()==2){
                holder.txt_state.setText("(已就诊)");
            }else if (Appointlist.get(i).getAppointment_state()==5){
                holder.txt_state.setText("(未手术)");
            }else if (Appointlist.get(i).getAppointment_state()==7){
                holder.txt_state.setText("(已手术)");
            }else if (Appointlist.get(i).getAppointment_state()==10){
                holder.txt_state.setText("(已手术)");
            }
            if (Appointlist.get(i).getAppoint_date()!=""){
                SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd ");
//                sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                String time = Appointlist.get(i).getAppoint_date();
                long l = Long.parseLong(time);
                Date date = new Date(l);
                String appointment_time = sdf.format(date);
                holder.txt_date.setText(appointment_time);
            }
            return view;
        }
        class ViewHolder{
            TextView txt_number,txt_date,txt_state,txt_name;
        }
    }
}
